<?php
$sort = $this->sort;
$sort_code = array("class"=>"","rel"=>"asc");
$sort_name = array("class"=>"","rel"=>"asc");
$sort_price = array("class"=>"","rel"=>"asc");
$sort_date = array("class"=>"","rel"=>"asc");
$sort_hot   = array("class"=>"","rel"=>"asc");
$sort_new   = array("class"=>"","rel"=>"asc");
$sort_sale   = array("class"=>"","rel"=>"asc");
$sort_bargain= array("class"=>"","rel"=>"asc");
$sort_dealset= array("class"=>"","rel"=>"asc");

switch($sort['by']){    
    case "code": 
        $sort_code = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "name": 
        $sort_name = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "price": 
       $sort_price = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break; 
    case "isHot":
       $sort_hot = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "isNew":
       $sort_new = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "isSale":
       $sort_sale = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "isBargain":
       $sort_bargain = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "isDealset":
       $sort_dealset = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
}
?>
<table class="data display datatable">
    <thead>
        <tr>
            <th width="100" class="center sorting<?=$sort_code['class']?>"><a href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"list","sortby"=>"code"))?>" rel="<?=$sort_code["rel"]?>">Mã sản phẩm</a></th>
            <th class="center">Hình</th>
            <th class="sorting<?=$sort_name['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"list","sortby"=>"name"), null, true)?>" rel="<?=$sort_name["rel"]?>">Tên sản phẩm</a>
            </th>
            <th width="120" class="sorting<?=$sort_price['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"list","sortby"=>"price"), null, true)?>" rel="<?=$sort_price['rel']?>">Giá</a>
            </th>
            
            <th width="50" class="center sorting<?=$sort_hot['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"list","sortby"=>"isHot"), null, true)?>" rel="<?=$sort_hot['rel']?>">Hot</a>
            </th>
            
            <th width="50" class="center sorting<?=$sort_new['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"list","sortby"=>"isNew"), null, true)?>" rel="<?=$sort_new['rel']?>">New</a>
            </th>
            
            <th width="50" class="center sorting<?=$sort_sale['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"list","sortby"=>"isSale"), null, true)?>" rel="<?=$sort_sale['rel']?>">Sale</a>
            </th>
            
            <th width="50" class="center sorting<?=$sort_bargain['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"list","sortby"=>"isBargain"), null, true)?>" rel="<?=$sort_bargain['rel']?>">Bargain</a>
            </th>
            
            <th width="70" class="sorting<?=$sort_dealset['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"list","sortby"=>"isDealset"), null, true)?>" rel="<?=$sort_dealset['rel']?>">Deal Set</a>
            </th>
            
            <th width="60" class="center">Sửa</th>
            <th width="50" class="center">Xóa</th>
        </tr>                   
    </thead>
    <tbody>
    <?php $stt=1; foreach($this->datas as $row){ $parity = ($stt%2)?"odd":"even";?>
        <tr class="gradeA <?=$parity?>">
            <td><?=$row->code?></td>
            <td class="center" width="50">
            <?php
            $_src = $this->baseUrl("images/product/thumb/$row->thumb");
            $_href = $this->baseUrl("images/product/$row->image");
            $img = empty($row->thumb)?"":"<a class='fancybox'  title='$row->name' href='$_href'><img src='$_src' width='30' height='35' alt='$row->name' /></a>";
            echo $img;
            ?>
            </td>
            <td><?=$row->name?></td>
            <td>$<?=$row->price?></td>
            <td class="center">
                <a class="choice-multi" rel="<?=$row->isHot?>" href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"index","choice"=>"isHot","id"=>$row->id))?>">
                    <?php $ico = ($row->isHot)?"star_list.png":"star_unlist.png"?>
                    <img src="<?=$this->baseUrl("templates/admin/img/$ico")?>" />
                </a>
            </td>
            <td class="center">
                <a class="choice-multi" rel="<?=$row->isNew?>" href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"index","choice"=>"isNew","id"=>$row->id))?>">
                    <?php $ico = ($row->isNew)?"star_list.png":"star_unlist.png"?>
                    <img src="<?=$this->baseUrl("templates/admin/img/$ico")?>" />
                </a>
            </td>
            <td class="center">
                <a class="choice-multi" rel="<?=$row->isSale?>" href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"index","choice"=>"isSale","id"=>$row->id))?>">
                    <?php $ico = ($row->isSale)?"star_list.png":"star_unlist.png"?>
                    <img src="<?=$this->baseUrl("templates/admin/img/$ico")?>" />
                </a>
            </td>
            <td class="center">
                <a class="choice-multi" rel="<?=$row->isBargain?>" href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"index","choice"=>"isBargain","id"=>$row->id))?>">
                    <?php $ico = ($row->isBargain)?"star_list.png":"star_unlist.png"?>
                    <img src="<?=$this->baseUrl("templates/admin/img/$ico")?>" />
                </a>
            </td>
            <td class="center">
                <a class="choice-multi" rel="<?=$row->isDealset?>" href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"index","choice"=>"isDealset","id"=>$row->id))?>">
                    <?php $ico = ($row->isDealset)?"star_list.png":"star_unlist.png"?>
                    <img src="<?=$this->baseUrl("templates/admin/img/$ico")?>" />
                </a>
            </td>
            
            <td class="center">
                <a class="edit" href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"edit","id"=>$row->id), null, true)?>">
                    <img src="<?=$this->baseUrl("templates/admin/img/icon-edit.png")?>" alt="[Sửa]" title="Sửa"/>
                </a>
            </td>
            <td class="center">
                <a class="del" href="<?=$this->url(array("module"=>"admin","controller"=>"product","action"=>"del","id"=>$row->id), null, true)?>">
                    <img src="<?=$this->baseUrl("templates/admin/img/icon-del.png")?>" alt="[Xóa]" title="Xóa"/>
                </a>
            </td>
        </tr>
    <?php $stt++;}?>
    </tbody>
</table>
<?php if($this->datas->count()>1){// Nhiều hơn 1 trang thì sử dụng phân trang?>
<div class="dataTables_paginate paging_full_numbers">
    <?=$this->paginationControl($this->datas,"Sliding","partial/pagination-control.phtml")?>
</div>
<?php }?>
<div class="dialog-message" title="Thông báo" style="display: none;"></div>
<script>
$(function(){
        
    /*Xóa bài viết*/
    $(".del").click(function(){
        var _this = $(this);
        $(".dialog-message").html("<div class='message warning'><h5>Cảnh báo</h5><p>Bạn có chắc muốn thực hiện hành động này không?</p></div>")
                            .dialog({            
            autoOpen: true,
            modal: true,
            buttons: {
                "Có": function(){                    
                    var link =  _this.attr("href");
                    _this.parent("td").parent("tr").remove();
                    $.get(link,{},function(){},"json");
                    $(this).dialog("close");
                },
                "Không": function(){
                    $(this).dialog("close");
                }
            }
        })
        return false;
    })
    
     /*Phân trang*/
    $(".pagination a").click(function(){
        var link = $(this).attr("href");
        $(".loading").show();
        $.get(link,null,function(rdata){
            $(".dataTables_wrapper").html(rdata);
            $(".loading").fadeOut(1000);
        })
        return false;
    })
    
    /*Sắp xếp*/
    $(".sorting a").click(function(){
        var link = $(this).attr("href");
        var type = $(this).attr("rel");
        var filter_value = $(".searchkey").val();
        var sdata = {"sorttype":type,"filter_field":"name","filter_value":filter_value};
        if(filter_value.length==0){
            sdata={"sorttype":type};
        }
        $(".loading").show();
        $.post(link,sdata,function(rdata){
            $(".dataTables_wrapper").html(rdata);
            $(".loading").fadeOut(1000);
        })
        return false;
    })
    
    /*thay đổi trạng thái*/
    $(".choice-multi").click(function(){
        var icon = $(this).children("img").attr("src");
        var status = $(this).attr("rel");
        var link = $(this).attr("href");
        if(status==0){            
            var n_icon = icon.replace("star_unlist","star_list");
            $(this).attr("rel",1);
        }else{
            var n_icon = icon.replace("star_list","star_unlist");
            $(this).attr("rel",0);
        }
        $(this).children("img").attr("src",n_icon);
        $.get(link,{},function(){},"json");
        return false;
    })
    
    /*fancybox*/
    
    $("a.fancybox").fancybox({
		'overlayShow'	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});   
    
})
</script>